using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System;

public delegate void ScoreEvent(int s);

public static class GlobalEventManager
{
    //游戏开始事件
    public static event Action OnGameStart;
    public static void NotifyGameStart()
    {
        OnGameStart();
    }

    //游戏重新开始事件
    public static event Action OnGameRestart;
    public static void NotifyGameRestart()
    {
        OnGameRestart();
    }

    //分数变化事件
    public static event ScoreEvent OnScoreChange;
    public static void NotifyScoreChange(int s)
    {
        OnScoreChange(s);
    }

    //游戏结束事件
    public static event Action OnGameOver;
    public static void NotifyGameOver()
    {
        OnGameOver();
    }

    //游戏暂停事件
    public static event Action OnGamePause;
    public static void NotifyGamePause()
    {
        OnGamePause();
    }

    //游戏继续事件
    public static event Action OnGameContinue;
    public static void NotifyGameContinue()
    {
        OnGameContinue();
    }

    //以下事件由其他脚本触发，UISystem负责接收来自其他脚本的请求
    //发送暂停请求
    public static event Action PauseRequest;
    public static void SendPauseRequest()
    {
        PauseRequest();
    }

    //发送继续游戏请求
    public static event Action ContinueRequest;
    public static void SendContinueRequest()
    {
        ContinueRequest();
    }

    //发送重新开始游戏请求
    public static event Action RestartRequest;
    public static void SendRestartRequest()
    {
        RestartRequest();
    }

    public static event Action ExitRequest;
    public static void SendExitRequest()
    {
        ExitRequest();
    }
}

public class UISystem : MonoBehaviour
{
    //游戏开始UI
    [SerializeField]
    private GameObject startUI;

    //游戏运行中UI
    [SerializeField]
    private GameObject gameRunningUI;

    //游戏结束UI预制体及实例
    [SerializeField]
    private GameObject GameEndUI;
    private GameObject GameEndUIInstance;

    [SerializeField]
    private GameObject ExitPopup;
    private GameObject ExitPopupInstance;

    //鸟
    [SerializeField]
    private GameObject bird;
    private BirdControll birdControll;

    //障碍物生成器
    [SerializeField]
    private GameObject blockGeneratorObject;
    private BlockGenerator blockGenerator;

    //分数
    public int Score { get; private set; }

    //历史分数
    [SerializeField]
    private GameObject saveScore;

    //游戏是否开始
    private bool gameIsRun = false;

    // Start is called before the first frame update
    void Start()
    {
        //初始化鸟控制器,障碍物生成控制器
        birdControll = bird.GetComponent<BirdControll>();
        blockGenerator = blockGeneratorObject.GetComponent<BlockGenerator>();

        //监听鸟与障碍物的事件
        birdControll.BirdDie += GameOver;
        blockGenerator.CrossBlock += AddScore;

        //监听其他脚本发出的全局请求
        GlobalEventManager.PauseRequest += Pause;
        GlobalEventManager.ContinueRequest += Continue;
        GlobalEventManager.RestartRequest += ReStart;
        GlobalEventManager.ExitRequest += () => {Application.Quit(); };
    }

    // Update is called once per frame
    void Update()
    {
        //当游戏未开始时，按下空格开始游戏
        if (!gameIsRun && Input.GetMouseButtonDown(0))
        {
            StartGame();
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if(ExitPopupInstance == null)
            {
                Time.timeScale = 0;
                ExitPopupInstance = Instantiate(ExitPopup);
            }
        }
    }

    //游戏开始
    public void StartGame()
    {
        GlobalEventManager.NotifyGameStart();
        gameRunningUI.SetActive(true);
        gameIsRun = true;
        startUI.SetActive(false);
    }


    //游戏暂停
    public void Pause()
    {
        Time.timeScale = 0;
        GlobalEventManager.NotifyGamePause();
    }

    //游戏继续
    public void Continue()
    {
        Time.timeScale = 1;
        GlobalEventManager.NotifyGameContinue();
    }

    //游戏结束
    public void GameOver()
    {
        Time.timeScale = 0;

        saveScore.GetComponent<SaveScore>().currentScore = Score;
        //触发GameOver事件
        GlobalEventManager.NotifyGameOver();

        //创建并初始化游戏结束UI
        GameEndUIInstance = Instantiate(GameEndUI);
        GameEndUIControll gameEndUIControll = GameEndUIInstance.GetComponent<GameEndUIControll>();
        gameEndUIControll.score = Score;
        gameEndUIControll.historyScore = saveScore.GetComponent<SaveScore>().historyBestScore;
        gameEndUIControll.scoreNum = saveScore.GetComponent<SaveScore>().saveScoreNum;
    }

    //加分
    public void AddScore(int addValue)
    {
        Score += 1;
        GlobalEventManager.NotifyScoreChange(Score);
    }

    //重新开始，包括游戏暂停后及游戏结束后的重新开始。
    public void ReStart()
    {
        gameIsRun = false;
        Time.timeScale = 1;
        Score = 0;

        //处理暂停,游戏结束，开始UI，分数
        if (GameEndUIInstance != null)
        {
            Destroy(GameEndUIInstance);
        }
        startUI.SetActive(true);
        
        //触发重新开始事件,重置场景中的物体
        GlobalEventManager.NotifyGameRestart();
    }
}
